package com.example.weblending.controlller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.weblending.domain.ApplyCondition;
import com.example.weblending.domain.ApplyNum;
import com.example.weblending.domain.ApplyUnset;
import com.example.weblending.domain.User;
import com.example.weblending.exception.GlobalException;
import com.example.weblending.service.ApplyConditionService;
import com.example.weblending.service.ApplyNumService;
import com.example.weblending.service.ApplyUnsetService;
import com.example.weblending.service.UserService;
import com.example.weblending.utils.JwtUtils;
import com.example.weblending.utils.Result;
import com.example.weblending.vo.AnalyzeColumn;
import com.example.weblending.vo.CsvInputColumn;
import com.example.weblending.vo.UnsetPassword;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/apply/condition")
@CrossOrigin
public class ApplyConditionController {
    @Resource
    private UserService userService;
    @Resource
    private ApplyConditionService applyConditionService;

    /**
     * 获得自己的成功率查询申请列表 （重点： 还没有被处理的）
     */
    @GetMapping("list/{page}/{limit}")
    public Result list(@PathVariable long page, @PathVariable long limit, HttpServletRequest request){
        User user=JwtUtils.checkUser(request,userService);
        Page<ApplyCondition> conditionPage =applyConditionService.getList(page,limit,user);
        return Result.ok().data(conditionPage).data("item",conditionPage.getRecords());
    }


    /**
     * 获得自己的成功率查询申请列表 （重点： 已经被处理的）
     */
    @GetMapping("history/{page}/{limit}")
    public Result history(@PathVariable long page, @PathVariable long limit, HttpServletRequest request){
        User user=JwtUtils.checkUser(request,userService);
        Page<ApplyCondition> conditionPage =applyConditionService.getHistory(page,limit,user);
        return Result.ok().data(conditionPage).data("item",conditionPage.getRecords());
    }


    /**
     * 获得等待处理的查询列表
     */
    @GetMapping("examine/{page}/{limit}")
    public Result examine(@PathVariable long page, @PathVariable long limit, HttpServletRequest request){
        User user=JwtUtils.isOperator(request,userService);
        Page<ApplyCondition> conditionPage =applyConditionService.getExamine(page,limit,user);
        return Result.ok().data(conditionPage).data("item",conditionPage.getRecords());
    }


    /**
     * 获得自己已经处理的查询列表
     */
    @GetMapping("approve/{page}/{limit}")
    public Result approve(@PathVariable long page, @PathVariable long limit, HttpServletRequest request){
        User user=JwtUtils.isOperator(request,userService);
        Page<ApplyCondition> conditionPage =applyConditionService.getApprove(page,limit,user);
        return Result.ok().data(conditionPage).data("item",conditionPage.getRecords());
    }

    /**
     * 新建成功率查询申请
     */
    //登录
    @PostMapping("")
    public Result create(@RequestBody CsvInputColumn condition, HttpServletRequest request){
        User user=JwtUtils.checkUser(request,userService);
        ApplyCondition applyCondition=applyConditionService.apply(condition,user);
        //异步的查询该申请的成功率
        applyConditionService.refreshAsync(applyCondition);
        return Result.ok();
    }

    /**
     * 通过该申请
     */
    @PostMapping("/pass/{id}")
    public Result pass(@PathVariable int id,HttpServletRequest request){
        User user=JwtUtils.isOperator(request,userService);
        boolean success=applyConditionService.setState(id,1,user);
        return success?Result.ok():Result.error();
    }

    /**
     * 拒绝该申请
     */
    @PostMapping("deny/{id}")
    public Result deny(@PathVariable int id,HttpServletRequest request){
        User user=JwtUtils.isOperator(request,userService);
        boolean success=applyConditionService.setState(id,-1,user);
        return success?Result.ok():Result.error();
    }

    /**
     * 重新刷新成功率
     */
    //增加分析次数
    @PostMapping("refresh/{id}")
    public Result refresh(@PathVariable String id,HttpServletRequest request){
        User user=JwtUtils.checkUser(request,userService);
        boolean success=applyConditionService.refresh(id,user);
        return success?Result.ok():Result.error();
    }

    /**
     * 删除申请
     */
    //增加分析次数
    @DeleteMapping("")
    public Result delete(@RequestBody int[] list, HttpServletRequest request){
        User user=JwtUtils.checkUser(request,userService);
        boolean success = applyConditionService.delete(list,user);
        return success?Result.ok():Result.error();
    }

}
